/**
 * TMP001新增
 * */


var formArys = {
    title: '', //店名
    describe: '', //描述
    img: '' //门头
}; //表单对象


//新增表单
function formAdd(){
    if (!vid) {
        msgPop({msg: '没有视频ID'}); return
    }
    var h = '';
    //店名
    h += '<table>';
    h += '<tr>';
    h += '<td class="tl">店名</td>';
    h += '<td class="tr">';
    h += '<input type="text" id="title" placeholder="输入店名" onclick="inputClick(this);" onblur="inputBlur();"/>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //描述
    h += '<table>';
    h += '<tr>';
    h += '<td class="tl">描述</td>';
    h += '<td class="tr">';
    h += '<textarea id="describe" placeholder="想说点什么" onclick="txtClick(this);" onblur="txtBlur();"></textarea>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //图片
    h += '<table id="tb-img">';
    h += '<tr>';
    h += '<td class="tl" style="vertical-align: top;">门头</td>';
    h += '<td class="tr">';
    h += '<div id="uploadOut" style="width: 100px; height: 25px; background: red; overflow: hidden;">';
    h += uploadHtml();
    h += '</div>';
    h += '<div id="imgs" style="margin-top: 5px; max-width: 150px; max-height: 400px; overflow: auto;"></div>';
    h += '</td>';
    h += '</tr>';
    h += '</table>';
    //按钮
    h += '<div id="tb-btn" onclick="formSave();">保存</div>';
    $('#form').html(h);
}


//点击激活
function inputClick(e){
    $('#' + e.id).addClass('click');
}


//失去激活
function inputBlur(){
    var title = $('#title');
    var val = title.val();
    title.removeClass('click');
    if (val != '') {
        formArys.title = val;
    }
    console.log(formArys);
}


//点击激活
function txtClick(e){
    $('#' + e.id).addClass('txtClick');
}


//失去激活
function txtBlur(){
    var describe = $('#describe');
    var val = describe.val();
    describe.removeClass('txtClick');
    if (val != '') {
        formArys.describe = val;
    }
    console.log(formArys);
}


//上传HTML
function uploadHtml(){
    var h = '';
    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; position: relative;">上传图片</div>';
    h += '<input type="file" id="uploadFile" style="width: 105px; height: 25px; opacity: 0; position: relative; top: -26px;" onchange="uploadImage()"/>';
    return h;
}


//图片HTML
function imgHtml(){
    var h = '';
    h += '<div style="margin-bottom: 5px; width: 150px; height: 100px; overflow: hidden;">';
    h += '<img src="' + formArys.img + '" style="width: 150px;"/>';
    h += '<a href="javascript:;" style="padding: 5px 10px; color: #fff; background: red; font-size: 12px; border: 0; border-radius: 0; opacity: 0.7; position: relative; top: -110px; left: 80px;" onclick="imgDel();">点击删除</a>';
    h += '</div>';
    return h;
}


//显示图片
function showImg(e){
    formArys.img = e;
    $('#imgs').html(imgHtml());
    $('#uploadOut').html(uploadHtml());
}


//删除图片
function imgDel(){
    formArys.img = '';
    $('#imgs').html(imgHtml());
}


//保存表单
function formSave(){
    var userInfo = getUserInfo();
    if (formArys.title == '') {
        msgPop({msg: '没有店名'}); return
    }
    var formData = {
        uid: getUid(code, userInfo),
        vid: vid,
        code: code,
        phone: userInfo.phone,
        title: formArys.title,
        describe: formArys.describe,
        head: formArys.img
    };
    console.log(formData);
    $.ajax({
        type: apis.shop1Add.method,
        url: domain + apis.shop1Add.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '保存成功'});
            window.location.href = routes.shop1One + '?code=' + code + '&shopCode=' + res.data;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//用户手机号
function viewPhone(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 20px; width: ' + (cw * 0.6) + 'px; max-height: 400px; text-align: center; background: #fff; color: #333; border-radius: 5px; overflow-x: hidden; overflow-y: auto; position: fixed; top: 15%; left: ' + ((cw * 0.4 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">设置手机号</div>';
    h += '<style type="text/css">.pgroup{margin: 10px auto;} .pgroup input{padding: 10px; border-radius: 4px; border: 1px solid #aaa; outline: none; font-size: 14px;} .pgroup button{padding: 5px 0; width: 100%; border: 0; border-radius: 2px; background: #0088cc; color: #eee;}</style>';
    //手机号
    h += '<div class="pgroup">';
    h += '<input type="text" placeholder="输入手机号" id="phone" value="150505050"/>';
    h += '</div>';
    //发送验证码
    h += '<div class="pgroup">';
    h += '<button type="button" onclick="sendYzm();">发送验证码</button>';
    h += '</div>';
    //验证码
    h += '<div class="pgroup">';
    h += '<input type="text" placeholder="输入验证码" id="yzm"/>';
    h += '</div>';
    //按钮
    h += '<div class="pgroup">';
    h += '<button type="button" onclick="setPhone();">保存手机号</button>';
    h += '</div>';
    h += '</div>';
    h += '</div>';
    $('#phonePop').html(h);
}


//发送验证码
function sendYzm(){
    var userInfo = getUserInfo();
    var phone = $('#phone').val();
    if (phone.length < 11) {
        msgPop({msg: '手机号不能少于11位'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        phone: phone
    };
    $.ajax({
        type: apis.sendYzm.method,
        url: domain + apis.sendYzm.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '验证码成功'});
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//保存手机号
function setPhone(){
    var userInfo = getUserInfo();
    var phone = $('#phone').val();
    var yzm = $('#yzm').val();
    if (phone.length < 11) {
        msgPop({msg: '手机号不能少于11位'});
        return
    }
    if (yzm == '') {
        msgPop({msg: '验证码必须的'});
        return
    }
    var formData = {
        uid: getUid(code, userInfo),
        userId: userInfo.userId,
        phone: phone,
        yzm: yzm
    };
    $.ajax({
        type: apis.userPhone.method,
        url: domain + apis.userPhone.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            userInfo.phone = phone;
            setUserInfo({val: userInfo});
            msgPop({msg: '设置成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}